<?php
/**
 * Author: Nguyen Hoai Tan (nguoiao007@gmail.com)
 * 
 * @theme   default 
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 * 
 */
require_once 'Zend/Config.php';
require_once 'Zend/Config/Writer/Array.php';

class Setting_AdminController extends Nine_Controller_Action_Admin {
	
	public function indexAction() {
		
		$this->view->headTitle(Nine_Language::translate('Settings'));
        $this->view->menu = array('setting');
        
        /**
         * Get old setting
         */
        $oldSettings = include 'config.php';
        
        /**
         * Get data
         */
        $data = $this->_getParam ( 'data', false );
        $errors = array();
        if (false != @$data) {
        	$newSettings = $oldSettings;
        	
        	/**
        	 * Site settings
        	 */
        	if(null != @$data['offline']) {
        		$newSettings['offline'] = $data['offline'];
        	}
        	if(null != @$data['offline_message']) {
        		$newSettings['offlineMessage'] = $data['offline_message'];
        	}
        	if(null != @$data['site_name']) {
        		$newSettings['websiteName'] = $data['site_name'];
        	}
        	if(null != @$data['live_site']) {
        		$newSettings['liveSite'] = $data['live_site'];
        	}
        	if(null != @$data['meta_description']) {
        		$newSettings['metaDescription'] = $data['meta_description'];
        	}
        	if(null != @$data['meta_keywords']) {
        		$newSettings['metaKeywords'] = $data['meta_keywords'];
        	}
        	
        	/**
        	 * Database settings
        	 */
        	if(null != @$data['host']) {
        		$newSettings['database']['params']['host'] = $data['host'];
        	}
        	if(null != @$data['username']) {
        		$newSettings['database']['params']['username'] = $data['username'];
        	}
        	if(null != @$data['password']) {
        		$newSettings['database']['params']['password'] = $data['password'];
        	}
        	if(null != @$data['dbname']) {
        		$newSettings['database']['params']['dbname'] = $data['dbname'];
        	}
        	if(null != @$data['prefix']) {
        		$newSettings['database']['params']['prefix'] = $data['prefix'];
        	}
        	
        	/**
        	 * Save config
        	 */
        	try {
        		$writer = new Zend_Config_Writer_Array();
        		$writer->setExclusiveLock(true);
				$writer->write('config.php' , new Zend_Config($newSettings));
				$writer->setExclusiveLock(false);
				
				$this->session->settingMessage = array(
												'success' => true,
												'message' => Nine_Language::translate('Settings is apply successfully.')
												);
				
				$this->_redirect('setting/admin/index');
				
        	} catch(Exception $e) {
        		$this->session->settingMessage = array(
												'success' => true,
												'message' => Nine_Language::translate('Error when apply new settings configuring.')
												);
        	}
        	
        }
        else {
        	$data = $oldSettings;
        	$data['offline_message'] = @$oldSettings['offlineMessage'];
			$data['host'] = @$oldSettings['database']['params'] ['host'];
			$data['username'] = @$oldSettings['database']['params']['username'];
			$data['password'] = @$oldSettings['database']['params']['password'];
			$data['dbname'] = @$oldSettings['database']['params']['dbname'];
			$data['prefix'] = @$oldSettings['database']['params']['prefix'];
			$data['site_name'] = @$oldSettings['websiteName'];
			$data['live_site'] = @$oldSettings['liveSite'];
			$data['meta_description'] = @$oldSettings['metaDescription'];
			$data['meta_keywords'] = @$oldSettings ['metaKeywords'];
        }
        
        /**
         * Assign to view
         */
		$this->view->data = $data;
		$this->view->errors =$errors;
		$this->view->settingMessage = $this->session->settingMessage;
		$this->session->settingMessage = null;
	
	}

}